﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Drawing;
using ThoughtWorks.QRCode.Codec;
using System.Text;

namespace lcs.Web
{
    public partial class PrintQCR : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request["id"]))
                {
                    string id = Request["id"];

                    if (Request["type"] == "ins")//库存入库打印
                    {
                        lcs.BLL.instore bll = new lcs.BLL.instore();

                        DataSet ds = bll.GetinstoreByWhere("  and iid= " + id);

                        if (ds.Tables[0].Rows.Count == 1)
                        {
                            DataRow r = ds.Tables[0].Rows[0];

                            // string str = "项目名称:" + r["projectname"]
                            //+ "，物料描述：" + r["material"]
                            //+ "，供应商：" + r["supplier"]
                            //+ "，联系人：" + r["suppliername"]
                            //+ "，联系电话：" + r["supplierphone"]
                            //+ "，仓库：" + r["sname"]
                            //+ "，仓位：" + r["spname"]
                            //+ "，入库日期：" + r["intime"]
                            //+ "，数量：" + r["innum"];
                            string str = "" + r["mid"];

                            CreateQRCode(str, id);
                        }
                    }
                    else if (Request["type"] == "returnGoods")
                    {
                        DataSet ds = new lcs.BLL.returnGoods().GetList(" returnGoodsId=" + id);

                        if (ds.Tables[0].Rows.Count == 1)
                        {
                            DataRow r = ds.Tables[0].Rows[0];

                            string str = "" + r["mid"];

                            CreateQRCode(str, id);
                        }
                    }
                    else if (Request["type"] == "productionOrder")//生产步骤打印
                    {
                        CreateQRCode(id, "productionOrder" + id);
                    }
                    else if (Request["type"] == "out_detail")//出库分码打印
                    {
                        CreateQRCode(id, "out_detail" + id);
                    }
                    else if (Request["type"] == "mouthsked")//质量抽检编号打印
                    {
                        CreateQRCode(id, "mouthsked" + id);
                    }
                    else//出库打印
                    {
                        lcs.BLL.out_storage om = new BLL.out_storage();

                        DataSet ds = om.GetOutList(" out_storage_id =" + id);

                        if (ds.Tables[0].Rows.Count == 1)
                        {
                            DataRow r = ds.Tables[0].Rows[0];

                            // string str = "项目名称:" + r["projectname"]
                            //+ "，物料描述：" + r["material"]
                            //+ "，供应商：" + r["supplier"]
                            //+ "，联系人：" + r["suppliername"]
                            //+ "，联系电话：" + r["supplierphone"]
                            //+ "，仓库：" + r["sname"]
                            //+ "，仓位：" + r["spname"]
                            //+ "，出库日期：" + r["out_storage_Time"]
                            //+ "，数量：" + r["out_storage_count"];

                            string str = "" + r["mid"];

                            CreateQRCode(str, id);
                        }

                    }
                }
            }


        }

        private void CreateQRCode(string nr,string iid)
        {

            Bitmap bt;

            string enCodeString = nr;

            QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();

            //qrCodeEncoder.QRCodeVersion = 0;
            qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;//设置二维码编码格式  
            qrCodeEncoder.QRCodeScale = 4;//设置编码测量度

            qrCodeEncoder.QRCodeVersion = 0;//设置编码版本

            qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;//设置错误校验

            bt = qrCodeEncoder.Encode(enCodeString, Encoding.UTF8);

            string filename = iid + string.Format(DateTime.Now.ToShortTimeString(), "yyyymmddhhmmss") 

             + ".jpg";

            filename = filename.Replace(" ", "");

            filename = filename.Replace(":", "");

            filename = filename.Replace("-", "");

            filename = filename.Replace(".", "");

            bt.Save(Server.MapPath("~/QRCodeImg/") + filename + ".jpg");

            this.Image1.ImageUrl = "~/QRCodeImg/" + filename + ".jpg";

        }
    }
}